<?php


namespace App\Http\service;


use App\Engine\TokenService;
use Illuminate\Support\Facades\Log;

class TableService
{
   private $data;
   private $model;
   private $modelService;

   public function __construct($data)
   {
       $this->data=$data;
       $this->modelService=new ModelService($data["table"]);
       $this->model=$this->modelService->getModel();

   }

   public function getRule()
   {
       return $this->model->rule;
   }

   public function getRuleMessage()
   {
       return $this->model->ruleMessage;
   }

   public function insert()
   {
       $create=$this->model->filter_data($this->data);
       $res=$this->model->create($create);
       return $res;
   }

   public function update()
   {
       $update=$this->model->filter_data($this->data);
       Log::error($update);
       return $this->model->find($this->data["id"])->update($update);
   }

   public function del()
   {
       return $this->model->find($this->data["id"])->delete();
   }

   public function get()
   {

       return $this->model
           ->with($this->modelService->getWith())
           ->where($this->data["where"])
           ->orderBy($this->data["orderBy"],$this->data["asdes"])->paginate($this->data["num"]);
   }

   public function getUserOne()
   {
       return $this->model
           ->with($this->modelService->getWith())
           ->where($this->data["where"])
           ->where("uid",TokenService::getCurrentUid())
           ->orderBy($this->data["orderBy"],$this->data["asdes"])->first();
   }

   public function getOne()
   {
       return $this->model
           ->with($this->modelService->getWith())
           ->where($this->data["where"])
           ->orderBy($this->data["orderBy"],$this->data["asdes"])->first();
   }

   public function get_all()
   {
       return $this->model->select($this->data["select"])->get();
   }
}
